#!/bin/bash

#https://www.rabbitmq.com/getstarted.html

#1、环境配置

yum install ncurses ncurses-base ncurses-devel ncurses-libs ncurses-static ncurses-term ocaml-curses ocaml-curses-devel -y

yum install openssl-devel zlib-devel –y

yum -y install make ncurses-devel gcc gcc-c++ unixODBC unixODBC-devel openssl openssl-devel

#2、下载安装erlang

wget https://erlang.org/download/otp_src_22.0.tar.gz

tar -zxvf otp_src_22.0.tar.gz

chown -R root:root otp_src_22.0

cd otp_src_22.0
./configure --prefix=/opt/erlang --with-ssl -enable-threads -enable-smmp-support -enable-kernel-poll --enable-hipe --without-javac

##安装命令：
make&&make install

##要等10分钟左右。。。。。。。。。。。。

cat >> /etc/profile <<EOF

ERLANG_HOME=/opt/erlang
PATH=$ERLANG_HOME/bin:$PATH
export ERLANG_HOME
export PATH

EOF

source /etc/profile
erl -version

#3、下载安装 rabbitmq
#http://www.rabbitmq.com/changelog.html
wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.7.15/rabbitmq-server-generic-unix-3.7.15.tar.xz

yum -y install xz

#解压rabbitmq
xz -d rabbitmq-server-generic-unix-3.7.15.tar.xz
tar -xvf  rabbitmq-server-generic-unix-3.7.15.tar
cd /opt/rabbitmq_server-3.7.15/
./sbin/rabbitmq-server



#启动命令，该命令ctrl+c后会关闭服务
./sbin/rabbitmq-server
#在后台启动Rabbit
./sbin/rabbitmq-server -detached

./rabbitmqctl add vhost test_host

#需要在启动的情况下面执行下面的语句
./sbin/rabbitmqctl add_user ning NingRabbit_123
./sbin/rabbitmqctl set_user_tags ning administrator
./sbin/rabbitmqctl set_permissions -p / ning ".*" ".*" ".*"
./sbin/rabbitmqctl set_permissions -p test_host ning ".*" ".*" ".*"

#开启插件管理页面
./sbin/rabbitmq-plugins enable rabbitmq_management
#关闭服务
./sbin/rabbitmqctl stop

#http://192.168.202.10:15672/
#


./rabbitmqctl status |grep rabbit
./rabbitmqctl list_users
./rabbitmqctl list_queues
./rabbitmqctl --node rabbit@node --vhost testVHost purge_queue testQueue
./rabbitmqctl --vhost / purge_queue test
./rabbitmqctl --vhost / delete_queue test


#下载rabbitmqadmin
#http://192.168.202.10:15672/cli
#上传到sbin 下面，然后chmod 755 rabbitmqadmin
#
./rabbitmqadmin -help

./rabbitmqadmin declare queue name=test_002  auto_delete=false durable=true -p test_host



#1.durable: 是否需要持久化, true表示持久化, false为非持久化
#2.auto_delete: 是否自动删除, true表示自动删除, false为非自动删除
#补充：
#如需指定vhost的队列(默认是/) 加上 --vhost

./rabbitmqadmin list queues
./rabbitmqadmin publish routing_key=test payload="hello world"
./rabbitmqadmin publish routing_key=test payload="hello world" -p test_host
./rabbitmqadmin list queues
./rabbitmqadmin get queue=test
./rabbitmqadmin list bindings


#--------------------cluster-------------------------------------
#修改每台节点
cat >> /etc/hosts <<EOF

192.168.202.10 ning-vm
192.168.202.11 ning-vm11

EOF

#修改每个集群节点的Erlang cookie为相同的值
vim /root/.erlang.cookie
chmod 700 /root/.erlang.cookie

#在 ning-vm11执行
./rabbitmqctl cluster_status
./rabbitmqctl stop_app
./rabbitmqctl join_cluster rabbit@ning-vm
./rabbitmqctl start_app

#下线节点，在删除的节点执行
./rabbitmqctl stop
rm -rf /opt/rabbitmq_server-3.7.15/var/lib/rabbitmq/mnesia/
#在主节点执行
rabbitmqctl  forget_cluster_node rabbit@ning-vm11



查看集群信息
#/opt/rabbitmq_server-3.7.15/var/lib/rabbitmq/mnesia/rabbit@ning-vm/cluster_nodes.config

./rabbitmqctl list_vhosts
./rabbitmqctl add_vhost test_host
